package com.yyaccp.easybuy.security.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yyaccp.easybuy.other.R;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

public class JsonUtil {
    private static ObjectMapper objectMapper = new ObjectMapper();

    public static void error(HttpServletResponse response, int responseStatus, String msg) {
        write(response, responseStatus, R.error(responseStatus, msg));
    }

    private static void write(HttpServletResponse response, int responseStatus, R result) {
        try {
            response.setContentType("application/json;charset=utf-8");
            response.setStatus(responseStatus);
            PrintWriter out = response.getWriter();
            out.write(objectMapper.writeValueAsString(result));
            out.flush();
            out.close();
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void ok(HttpServletResponse response, String msg) {
        write(response, HttpServletResponse.SC_OK, R.ok(msg));
    }
}
